package service

import (
	"context"
	"errors"
	"test/config"
	"test/discover"
)

var ErrNotServiceInstances = errors.New("instances are not existed")

type Service interface {
	HealthCheck() bool
	SavHello() string
	DiscoveryServer(ctx context.Context, serverName string) ([]interface{}, error)
}

type DiscoverServiceImpl struct {
	discoverClient discover.DiscoveryClient
}

var service discover.DiscoveryClient

func (service *DiscoverServiceImpl) SavHello() string {
	return "hello world"
}

func (service *DiscoverServiceImpl) DiscoveryServer(ctx context.Context, serviceName string) (
	[]interface{}, error) {
	instance := service.discoverClient.DiscoverService(serviceName, config.Logger)
	if instance == nil || len(instance) == 0 {
		return nil, ErrNotServiceInstances
	}
	return instance, nil
}

func (service *DiscoverServiceImpl) HealthCheck() bool {
	return true
}
